package com.hjb.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hjb.entity.User;
import com.hjb.mapper.UserMapper;
import com.hjb.service.UserService;
import com.hjb.uils.DateUtils;
import com.hjb.uils.MybatisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.jws.soap.SOAPBinding;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author JianBinHuang
 * @Description
 * @Date 2021/9/3 19:19
 */
@RequestMapping("/user")
@Controller
public class UserController{

    @Autowired
    UserService userService;


    @RequestMapping("/showAll")
    public String showAll(Integer pageIndex, HttpServletResponse response,Model model){
        if(pageIndex<1){
            pageIndex=1;
        }
        PageHelper.startPage(pageIndex,5);
        List<User> users = userService.queryAll();
        PageInfo<User> pageInfo=new PageInfo<>(users);
        model.addAttribute("pageInfo", pageInfo);
        return "forward:/show.jsp";
    }

    @RequestMapping("/deleteById")
    public String deleteById(@RequestParam("id") Integer id){
        int i = userService.deleteById(id);
        MybatisUtils.commitSession();
        return "redirect:/user/showAll?pageIndex=1";
    }


    @RequestMapping("/addUser")
    public String addUser( User user,HttpServletRequest request, HttpServletResponse response){
        String name = request.getParameter("name");
        String password = request.getParameter("password");
        String sex = request.getParameter("sex");
        Date birthday = DateUtils.stringToDate(request.getParameter("birthday"));
        Date registTime = DateUtils.stringToDate(request.getParameter("registTime"));

        user=new User(name,password,sex, birthday,registTime);

        userService.addUser(user);
        MybatisUtils.commitSession();

        return "redirect:/user/showAll?pageIndex=1";
    }

    /**
     * @Author JianBinHuang
     * @Date 2021/9/5 0:40
     * @param
     * @return
     * @Description
     *
     */
    @RequestMapping("/updateUser")
    public String updateUser(User user,HttpServletRequest request, HttpServletResponse response)  {
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String password = request.getParameter("password");
        String sex = request.getParameter("sex");
        String birthday = request.getParameter("birthday");
        String registTime = request.getParameter("registTime");

         user=new User(Integer.valueOf(id),name,password,sex, DateUtils.stringToDate(birthday),DateUtils.stringToDate(registTime));

        userService.update(user);

        MybatisUtils.commitSession();
        return "redirect:/user/showAll?pageIndex=1";
    }

    @RequestMapping(value = "updateUid")
    public String updateUid(Integer id, HttpServletResponse response,Model model){

        User user = userService.queryById(id);
        model.addAttribute("user", user);
        return "forward:/update.jsp";
    }


    @RequestMapping("/addJsp")
    public String addJsp(){
        return "forward:/add.jsp";
    }



}
